package com.credit.web.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.persistence.SearchDataTransform;

import com.credit.context.CreditServletContext;
import com.credit.entity.CreditLoanOrder;
import com.credit.entity.CreditPointsRecord;
import com.credit.entity.CreditUserInfo;
import com.credit.service.CreditPointsService;
import com.credit.util.BeanToMapUtil;
import com.credit.util.ParamsConstant;
import com.credit.web.basic.bean.ResBean;

@Controller
@RequestMapping("/points")
public class PointsController {
	@Autowired
	private CreditPointsService creditPointsService;
	
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public void list(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "page",defaultValue = "1") int pageNumber) throws IllegalArgumentException, IllegalAccessException {
		CreditUserInfo userInfo = CreditServletContext.getUserInfo();
		
		Map<String, Object> searchParams = new TreeMap<String, Object>();
		searchParams.put("EQ_userId", userInfo.getId());
		
		Map<String, SearchDataTransform> dataTyps = new HashMap<String, SearchDataTransform>();
		

		Page<CreditPointsRecord> page = creditPointsService.getPointsListPage(searchParams, pageNumber, ParamsConstant.PAGESIZE, "createTime", dataTyps);
		List<CreditPointsRecord> pointsRecordList = page.getContent();
		List<Map<String,Object>> pointsRecordListReturn = new ArrayList<Map<String,Object>>();
		for(CreditPointsRecord pointsRecord:pointsRecordList){
			pointsRecordListReturn.add(BeanToMapUtil.getMap(pointsRecord));
		}
		
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("pointsRecordList", pointsRecordListReturn);
		CreditServletContext.setResBean(resBean);
	}

}
